// 前端本地 Mock：调拨单 API

export const useSystransferorderApi = () => {
  return baseApi;
};

type Order = Systransferorder;
const mockDb: Order[] = [
  { id: 2200000000101, code: 'DBDH6131', documentStatus: 1, applyDate: '2024-02-29', fromWarehouse: '货品仓库一', toWarehouse: '货品仓库三', outStatus: 4, inStatus: 4, transferQuantity: 10, transferAmount: 500, agentName: '李依依', goodsName: '三锅头、可笑', remark: '草稿状态调拨单', tenantId: 1300000000001, orgId: 1300000000001, isDelete: false, deleteTime: null as any, createTime: '2024-02-29 09:00:00', updateTime: null as any, createUserId: 1300000000001, createUserName: '任杰', updateUserId: null as any, updateUserName: null as any },
  { id: 2200000000102, code: 'DBDH1907', documentStatus: 2, applyDate: '2024-02-29', fromWarehouse: '货品仓库一', toWarehouse: '货品仓库三', outStatus: 4, inStatus: 4, transferQuantity: 10, transferAmount: 500, agentName: '李依依', goodsName: '三锅头、可笑', remark: '待审核状态调拨单', tenantId: 1300000000001, orgId: 1300000000001, isDelete: false, deleteTime: null as any, createTime: '2024-02-29 10:00:00', updateTime: null as any, createUserId: 1300000000001, createUserName: '任杰', updateUserId: null as any, updateUserName: null as any },
  { id: 2200000000103, code: 'DBDH3489', documentStatus: 6, applyDate: '2024-02-29', fromWarehouse: '货品仓库一', toWarehouse: '货品仓库三', outStatus: 6, inStatus: 6, transferQuantity: 10, transferAmount: 500, agentName: '李依依', goodsName: '三锅头、可笑', remark: '已完成状态调拨单', tenantId: 1300000000001, orgId: 1300000000001, isDelete: false, deleteTime: null as any, createTime: '2024-02-29 11:00:00', updateTime: null as any, createUserId: 1300000000001, createUserName: '任杰', updateUserId: null as any, updateUserName: null as any },
];

const delay = (ms: number) => new Promise(r => setTimeout(r, ms));

const baseApi = {
  page: async (params: any) => {
    await delay(120);
    const { page = 1, pageSize = 20 } = params || {};
    const start = (page - 1) * pageSize;
    const items = mockDb.slice(start, start + pageSize);
    return { data: { result: { total: mockDb.length, items } } } as any;
  },
  list: async () => ({ data: { result: mockDb } } as any),
  detail: async (id: number) => {
    await delay(60);
    const item = mockDb.find(x => x.id === id);
    return { data: { result: item } } as any;
  },
  add: async (data: Order) => { data.id = Date.now(); mockDb.unshift(data); return { data: { result: true } } as any; },
  update: async (data: Order) => { const i = mockDb.findIndex(x => x.id === data.id); if (i >= 0) mockDb[i] = { ...mockDb[i], ...data }; return { data: { result: true } } as any; },
  delete: async (id: number) => { const i = mockDb.findIndex(x => x.id === id); if (i >= 0) mockDb.splice(i, 1); return { data: { result: true } } as any; },
  batchDelete: async (ids: number[]) => { ids.forEach(id => { const i = mockDb.findIndex(x => x.id === id); if (i >= 0) mockDb.splice(i, 1); }); return { data: { result: true } } as any; },
  importData: async () => ({ data: { result: true } } as any),
  downloadTemplate: async () => ({ data: { result: true } } as any),
};

export interface Systransferorder {
  id: number;
  code?: string;
  documentStatus?: number;
  applyDate?: string;
  fromWarehouse?: string;
  toWarehouse?: string;
  outStatus?: number;
  inStatus?: number;
  transferQuantity?: number;
  transferAmount?: number;
  agentName?: string;
  goodsName?: string;
  remark?: string;
  tenantId: number;
  orgId: number;
  isDelete?: boolean;
  deleteTime?: string | null;
  createTime?: string;
  updateTime?: string | null;
  createUserId?: number;
  createUserName?: string;
  updateUserId?: number | null;
  updateUserName?: string | null;
}


